package com.alibaba.otter.manager.web.resource;

import com.alibaba.otter.manager.web.dto.DelayStatInfoDTO;
import com.alibaba.otter.manager.web.dto.RestResponse;
import com.alibaba.otter.manager.web.dto.TopDelayStatDTO;
import com.alibaba.otter.manager.web.service.DelayStatAggService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.util.List;

@RequestMapping("/api/v1/delay-stat")
@RestController
public class DelayStatResource {


    private final DelayStatAggService delayStatAggService;

    public DelayStatResource(DelayStatAggService delayStatAggService) {
        this.delayStatAggService = delayStatAggService;
    }


    @GetMapping("/stat")
    public RestResponse<DelayStatInfoDTO> delayStat(@RequestParam(required = false,name = "startTime") Instant startTime,
                                                    @RequestParam(required = false,name = "endTime") Instant endTime,
                                                    @RequestParam("pipelineId") Long pipelineId) {

        return RestResponse.ok(delayStatAggService.delayStat(startTime, endTime, pipelineId));
    }


    @GetMapping("/topN")
    public RestResponse<List<TopDelayStatDTO>> topN(@RequestParam(required = false,name = "searchKey")String searchKey,
                                                    @RequestParam(required = false,name = "topN")Integer topN,
                                                    @RequestParam(required = false,name = "minute")Long minute) {
        return RestResponse.ok(delayStatAggService.topN(searchKey, topN, minute));
    }
}
